/**
 * 
 */
package com.ms.service.module.baccarat.db;

import java.util.ArrayList;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ms.game.common.GameType;
import com.ms.game.lobby.db.LobbyGameListWhileModel;
import com.xpec.c4.db.hibernate.manager.HibernateBusinessManager;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.game.utility.GameUtility;

/**
 * @author TW15010
 *
 */
public class BacrDAO {
	/**
	 * 讀取載入牌桌資料
	 * 
	 * @param tableId
	 * @return
	 */
	public static BacrMachineDataModel loadMachineModel(String tableId) {
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("FROM BacrMachineDataModel Where halltype=:HALLTYPE");
		query.setParameter("HALLTYPE", tableId);
		BacrMachineDataModel model = (BacrMachineDataModel) query.uniqueResult();
		return model;
	}

	/**
	 * 儲存牌桌資料
	 * 
	 * @param model
	 */
	public static void updateMachineModel(BacrMachineDataModel model) {
		if (model == null) {
			return;
		}

		Session s = HibernateManager.txBegin();
		s.saveOrUpdate(model);
		HibernateManager.txCommitFailRollbackClearSession();
	}

	/**
	 * 載入白名單列表
	 */
	public static ArrayList<LobbyGameListWhileModel> loadWhiteList() {
		Session session = HibernateBusinessManager.getSession();
		Query query = session.createQuery("from LobbyGameListWhileModel");
		@SuppressWarnings("unchecked")
		ArrayList<LobbyGameListWhileModel> list = (ArrayList<LobbyGameListWhileModel>) query.list();
		HibernateManager.endSession();

		return list;
	}
	
	/**
	 * 載入活動連勝資訊
	 */
	public static BacrActivityCombosInfoModel loadActivityCombosModel(String tableId){
		if(!GameUtility.isHaveString(tableId)){
			return null;
		}
		else{
			BacrActivityCombosInfoModel model = null;
			Session session = HibernateManager.getSession();
			Query query = session.createQuery("FROM BacrActivityCombosInfoModel Where tableID=:HALLTYPE");
			query.setParameter("HALLTYPE", tableId);
			model = (BacrActivityCombosInfoModel) query.uniqueResult();
			HibernateManager.endSession();
			return model;
		}
	}
	
	/**
	 * 新增一桌初始為關閉的活動連勝資料
	 */
	public static void insertNewActivityCombosInfo(String tableId){
		BacrActivityCombosInfoModel newModel = new BacrActivityCombosInfoModel();
		newModel.setTableID(tableId);
		
		newModel.setActive(false);
		newModel.setEffctiveCombosTime(0L);
		newModel.setMaxCombo(0);
		newModel.setStartTime(0L);
		newModel.setEndTime(0L);
		
		newModel.setComboStage1(0);
		newModel.setComboStage2(0);
		newModel.setComboStage3(0);
		newModel.setComboStage4(0);
		newModel.setComboStage5(0);
		
		newModel.setScoreStage1(0L);
		newModel.setScoreStage2(0L);
		newModel.setScoreStage3(0L);
		newModel.setScoreStage4(0L);
		newModel.setScoreStage5(0L);
		
		Session session = HibernateManager.txBegin();
		session.save(newModel);
		HibernateManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 建立儲存玩家 log 到 redis 時用的 key
	 * 
	 * @param tableId
	 * @param roundNumber
	 * @param playerId
	 * @return
	 */
	public static String createPlayerLogKeyOnRedis(String tableId, String roundNumber, String playerId) {
		if (tableId == null || roundNumber == null || playerId == null) {
			return "";
		}

		String key = GameType.BACR.name() + ":" + tableId + ":" + roundNumber + ":" + playerId;
		return key;
	}
}
